#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h> 
/*
int main() {
      int ret;
      char host[NI_MAXHOST];
      char server[NI_MAXSERV];
      struct sockaddr_in sa;
      memset(&sa, 0, sizeof(sa));
      sa.sin_family = AF_INET;
      inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr.s_addr);
      ret = getnameinfo((const struct sockaddr *)(&sa), sizeof(sa), host, sizeof(host), server, sizeof(server), 0);
      printf("servername %s \n", server);

}
*/


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
        int ret;
        char *ptr;
        char hostname[128] = {0};
        char servername[128] = {0};
        struct sockaddr_in addr_dst;

        ptr = argc < 2 ? "127.0.0.1" : argv[1];

        memset(&addr_dst,0,sizeof(addr_dst));
        addr_dst.sin_family = AF_INET;
        addr_dst.sin_addr.s_addr =  inet_addr(ptr);
        addr_dst.sin_port = htons(80);

        ret = getnameinfo((struct sockaddr *)&addr_dst, sizeof(addr_dst), hostname, sizeof(hostname), servername, sizeof(servername), 0);
        if (ret != 0)
        {
                fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
        }
        else
        {
                printf("hostname IP: %s \n", hostname);
                printf("servername: %s \n", servername);
        }


        exit(EXIT_SUCCESS);
}

